<?php
Class FT_Blog {
	private $info = array();
	private $url = array();
	private $users = array();
	private $user = null;
	private $cats = array();
	private $cat = null;
	private $extras;
	
	/* CONSTRUCTOR */
	
	public function FT_Blog($blog_id = null, $extras = true) {
		$this->info['id'] = $blog_id;
		$this->extras = $extras;
		
		$this->FT_Crear_Info();
		$this->FT_Crear_Usuarios();
		$this->FT_Crear_URL();
		
		$this->FT_Hooks();
	}
	
	/* METODOS PRIVADOS */
	
	/* Carga los Hooks */
	
	private function FT_Hooks() {
		add_action('wp', array($this, 'FT_Load_Theme'));
		add_action('wp', array($this, 'FT_Set_Cats'));
		add_action('admin_init', array($this, 'FT_Set_Cats'));
	}
	
	/* Crea la info basica del blog (ciudad) */
	
	private function FT_Crear_Info() {
		global $current_site, $current_blog;
		
		if(is_null($this->info['id'])) :
			$this->info = array(
				'id' => $current_blog->blog_id,
				'domain' => $current_blog->domain,
				'current' => true
			);
		else :
			$this->info['domain'] = substr(get_blog_option($this->info['id'], 'siteurl'), 7);
			$this->info['current'] = false;
			if($this->info['id'] == $current_blog->blog_id)
				$this->info['current'] = true;
		endif;
		
		$this->info['name'] = get_blog_option($this->info['id'], 'blogname');
		$this->info['long_name'] = 'First Tuesday '.$this->info['name'];
		
		if(strpos($this->info['domain'], $current_site->domain) !== false)
			$this->info['sub_domain'] = $this->FT_Subdominio();
		
		if($this->info['id'] == BP_ROOT_BLOG) :
			$this->info['long_name'] = $this->info['name'];
			$this->info['sub_domain'] = $this->info['domain'];
		endif;
	}
	
	/* Crea los usuarios del blog (ciudad) e identifica al usuario actual */
	
	private function FT_Crear_Usuarios() {
		global $wpdb, $current_user;
		
		if($this->info['current'] && $this->extras) :
			$sql = $wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = 'wp_{$this->info['id']}_capabilities';");
			$users_id = $wpdb->get_col($sql);
			
			if(!empty($users_id)) :
				foreach($users_id as $user_id) :
					$user_aux = new FT_User($user_id);
					$users_aux[strtolower($user_aux->FT_Get_Info('name'))] = $user_aux;
				endforeach;
				
				ksort($users_aux);
				
				foreach($users_aux as $user_aux) :
					$this->users[$user_aux->FT_Get_Info('id')] = $user_aux;
					if($user_aux->FT_Get_Info('current'))
						$this->user = $user_aux;
				endforeach;
			else :
				unset($this->users);
				if(!is_null($current_user->ID))
					$this->user = new FT_User();
				else
					unset($this->user);
			endif;
			
			$this->info['total_users'] = count($this->users);
		else :
			unset($this->users);
			unset($this->user);
		endif;
	}
	
	/* Crea las URL del blog (ciudad) */
	
	private function FT_Crear_URL() {
		$url_blog['base'] = 'http://'.$this->info['domain'];
		
		if(isset($this->user) && $this->user->FT_Get_Info('level') > 0) :
			$url_blog['admin'] = $url_blog['base'].'/wp-admin/';
			$url_blog['admin-post'] = $url_blog['admin'].'post-new.php';
			$url_blog['admin-comments'] = $url_blog['admin'].'edit-comments.php';
		endif;
		
		if($this->info['current'] && $this->extras) :
			$url_extra = array(
				'login' => $url_blog['base'].'/wp-login.php',
				BP_REGISTER_SLUG => $url_blog['base'].'/'.BP_REGISTER_SLUG.'/',
				BP_ACTIVATION_SLUG => $url_blog['base'].'/'.BP_ACTIVATION_SLUG.'/',
				FT_EVENTOS_SLUG => $url_blog['base'].'/'.FT_EVENTOS_SLUG.'/',
				FT_EMPRESAS_SLUG => $url_blog['base'].'/'.FT_EMPRESAS_SLUG.'/',
				BP_MEMBERS_SLUG => $url_blog['base'].'/'.BP_MEMBERS_SLUG.'/',
				BP_HOME_BLOG_SLUG => $url_blog['base'].'/'.BP_HOME_BLOG_SLUG.'/',
				FT_ORGANIZADORES_SLUG => $url_blog['base'].'/'.FT_ORGANIZADORES_SLUG.'/',
				FT_CONTACTAR_SLUG => $url_blog['base'].'/'.FT_CONTACTAR_SLUG.'/',
				FT_ACERCA_SLUG => $url_blog['base'].'/'.FT_ACERCA_SLUG.'/'
			);
			
			$url_blog = array_merge($url_blog, $url_extra);
		endif;
		
		$this->url = $url_blog;
	}
	
	/* Obtiene el subdominio del blog (ciudad) */
	
	private function FT_Subdominio() {
		$partes = explode('.', $this->info['domain']);
		
		return $partes[0];
	}
	
	/* Crea las categorias del blog (ciudad) */
	
	private function FT_Crear_Cats() {
		if($this->info['current']) :
			foreach(get_all_category_ids() as $cat_id) :
				$cat_aux = new FT_Cat($cat_id);
				$this->cats[$cat_aux->FT_Get_Info('name')] = $cat_aux;
				if($this->cats[$cat_aux->FT_Get_Info('name')]->FT_Get_Info('current'))
					$this->cat = $this->cats[$cat_aux->FT_Get_Info('name')];
			endforeach;
			
			if(!isset($this->cat)) :
				unset($this->cat);
			endif;
		else :
			unset($this->cats);
			unset($this->cat);
		endif;
	}
	
	/* METODOS PUBLICOS */
	
	/* Devuelve la info basica del blog (ciudad) */
	
	public function FT_Get_Info($info_name = '') {
		if(empty($info_name))
			return $this->info;
		else
			return $this->info[strtolower($info_name)];
	}
	
	/* Devuelve las URL del blog (ciudad) */
	
	public function FT_Get_URL($url_name = '') {
		if(empty($url_name))
			return $this->url;
		else
			return $this->url[strtolower($url_name)];
	}
	
	/* Devuelve el o los usuarios del blog (ciudad) */
	
	public function FT_Get_Users($user_id = null) {
		if(is_null($user_id))
			return $this->users;
		else
			return $this->users[$user_id];
	}
	
	/* Devuelve la o las categorias del blog (ciudad) */
	
	public function FT_Get_Cats($cat_name = '') {
		if(empty($cat_name))
			return $this->cats;
		else
			return $this->cats[$cat_name];
	}
	
	/* Establece el usuario actual */
	
	public function FT_Set_Current_User($user = null) {
		global $current_user;
		
		if(!is_null($user)) :
			if(is_numeric($user)) :
				$this->user = $this->FT_Get_Users($user);
			elseif(is_object($user) && get_class($user) == 'FT_User') :
				$this->user = $user;
			endif;
		elseif(isset($this->users) && !isset($this->user)) :
			foreach($this->users as $user_aux) :
				if($user_aux->FT_Get_Info('current')) :
					$this->user = $user_aux;
					break;
				endif;
			endforeach;
		endif;			
	}
	
	/* Crea las categorias del blog (ciudad) */
	
	public function FT_Set_Cats() {
		$this->FT_Crear_Cats();
	}
	
	/* Devuelve el usuario actual del blog (ciudad) */
	
	public function FT_Get_Current_User() {
		return $this->user;
	}
	
	/* Devuelve la categoria actual del blog (ciudad) */
	
	public function FT_Get_Current_Cat() {
		return $this->cat;
	}
	
	/* Carga el theme adecuado segun la URL actual */
	
	public function FT_Load_Theme() {
		switch($_SERVER['REQUEST_URI']) :
			case '/'.FT_EVENTOS_SLUG.'/' :
				$cat_id = get_cat_id('Events');
				if($cat_id == 0)
					$cat_id = get_cat_id('Eventos');
				
				if($cat_id != 0)
					query_posts('cat='.$cat_id);
					
				bp_core_load_template(FT_EVENTOS_SLUG.'/index', true);
				break;
		endswitch;
	} 
}
?>